package com.ruoyi.youtian.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.youtian.domain.FaultRecord;
import com.ruoyi.youtian.domain.FaultRecordDetail;
import com.ruoyi.youtian.domain.vo.*;
import com.ruoyi.youtian.mapper.FaultRecordDetailMapper;
import com.ruoyi.youtian.mapper.FaultRecordMapper;
import com.ruoyi.youtian.service.IFaultRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.xml.transform.Result;
import java.util.*;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 故障记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@Service
@Slf4j
public class FaultRecordServiceImpl implements IFaultRecordService
{
    @Autowired
    private FaultRecordMapper faultRecordMapper;

    @Autowired
    private FaultRecordDetailMapper faultRecordDetailMapper;

    /**
     * 查询故障记录
     *
     * @param recordId 故障记录主键
     * @return 故障记录
     */
    @Override
    public FaultRecordVO selectFaultRecordByRecordId(Long recordId)

    {
        FaultRecord faultRecord = faultRecordMapper.selectFaultRecordByRecordId(recordId);
        FaultRecordDetail faultRecordDetail=new FaultRecordDetail();
        faultRecordDetail.setRecordId(recordId);
        List<FaultRecordDetail> faultRecordDetails = faultRecordDetailMapper.selectFaultRecordDetailList(faultRecordDetail);
        FaultRecordVO faultRecordVO=new FaultRecordVO();
        faultRecordVO.setFaultRecord(faultRecord);
        faultRecordVO.setFaultRecordDetailList(faultRecordDetails);
        List<ReportDetailsVo> reportDetailsVos = faultRecordDetailMapper.selectReportDetailslists(recordId);

        for (ReportDetailsVo detailsVo : reportDetailsVos) {
            faultRecordVO.setReportDetailsVos(detailsVo);
            break;
        }

        return faultRecordVO;
    }

    /**
     * 查询故障记录列表
     *
     * @param faultRecord 故障记录
     * @return 故障记录
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<FaultRecord> selectFaultRecordList(FaultRecord faultRecord)
    {
        return faultRecordMapper.selectFaultRecordList(faultRecord);
    }



    /**
     * 查询故障记录列表
     * 故障记录集合
     * 功能修改添加字段
     * @param faultRecord 故障记录
     * @return 故障记录
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<FaultRecord> selectFaultRecordListUpdate(FaultRecord faultRecord) {

        List<FaultRecord> list = faultRecordMapper.selectFaultRecordList(faultRecord);


//        List<FaultRecordVo2> vo2List = faultRecordMapper.selectscenenanmeid(faultRecord);

        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(record->{
                // 原recordName  "异常名称,时间",进行拆解
                String[] split = record.getRecordName().split(",");
                if (split.length==2) {
                    record.setRecordName(split[0]);
                    try {
                        record.setCreateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, split[1]));
                    }catch (Exception e){

                    }

                    record.setCreateBy(split[1]);
                }
                //原fault_desc  "异常点位id,上限，下限，当前值",进行拆解
                String[] fault = record.getFaultDesc().split(",");
                if (fault.length <= 4) {
                    String[] strings = fault[3].toString().split(":");
                    record.setAcquisition(strings[1]);
                }
//                if (fault.length > 4) {
//                    String[] fa = fault[6].toString().split(":");
//                    record.setAcquisition(fa[1]);
//                }
//                vo2List.stream().forEach(moj ->{
//                    record.setDev_name(moj.getDev_name());
//                    record.setEjname(moj.getEjname());
//                    record.setDwmc(moj.getDwmc());
//                    record.setRecordName(moj.getDwmc());
//                });
            });
        }




        for (FaultRecord record : list) {
            if (record.getFaultDesc() != null) {

            String[] split = record.getFaultDesc().split(",");
            String[] id = split[0].toString().split(":");
            List<FaultRecordVo2> recordVo2s = faultRecordMapper.selectFaultRecorByid(id[1].toString());

            recordVo2s.stream().forEach(moj ->{
                List<FaultRecordVo2> faultRecordVo2s = faultRecordMapper.selectFaultRecorByids(moj.getId());
                faultRecordVo2s.stream().forEach(sVo2s ->{
                    record.setDev_name(sVo2s.getDev_name());
                });
            });
            List<FaultRecordVo2> recordVo2List =  faultRecordMapper.selectFaultRecortagByid(id[1]);
            recordVo2List.forEach(mo ->{
                record.setDwmc(mo.getDwmc());
                record.setRecordName(mo.getDwmc());
            });
            List<FaultRecordVo2> scenenlist = faultRecordMapper.selectcategory(id[1]);
            scenenlist.forEach(sc ->{
                List<FaultRecordVo2> sce = faultRecordMapper.selectscenenanme(sc.getSceneid());
                sce.stream().forEach(a->{
                    record.setEjname(a.getScene_name());
                });
            });
        }
        }
        return list;
    }



    /**
     * 新增故障记录
     *
     * @param faultRecord 故障记录
     * @return 结果
     */
    @Override
    public int insertFaultRecord(FaultRecord faultRecord)
    {
        return faultRecordMapper.insertFaultRecord(faultRecord);
    }

    /**
     * 修改故障记录
     *
     * @param faultRecord 故障记录
     * @return 结果
     */
    @Override
    public int updateFaultRecord(FaultRecord faultRecord)
    {
        return faultRecordMapper.updateFaultRecord(faultRecord);
    }

    /**
     * 批量删除故障记录
     *
     * @param recordIds 需要删除的故障记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultRecordByRecordIds(Long[] recordIds)
    {
        return faultRecordMapper.deleteFaultRecordByRecordIds(recordIds);
    }

    /**
     * 删除故障记录信息
     *
     * @param recordId 故障记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultRecordByRecordId(Long recordId)
    {
        return faultRecordMapper.deleteFaultRecordByRecordId(recordId);
    }


    @Override
    public int insertRecordAndDetail(FaultRecordVO faultRecordVO) {
        if (Objects.isNull(faultRecordVO)) {
            log.error("faultRecordVO 为 null");
            return 0;
        }

        FaultRecord faultRecord = faultRecordVO.getFaultRecord();

        if (Objects.isNull(faultRecord)) {
            log.error("faultRecord 为 null");
          return 0;
        }
           int  result = faultRecordMapper.insertFaultRecord(faultRecord);

        if (Objects.equals(0, result) || CollectionUtils.isEmpty(faultRecordVO.getFaultRecordDetailList())) {
            return 0;
        }

        faultRecordVO.getFaultRecordDetailList().forEach(detail->{
            detail.setRecordId(faultRecord.getRecordId());
            detail.setCreateTime(new Date());
            faultRecordDetailMapper.insertFaultRecordDetail(detail);
        });

        return  faultRecordVO.getFaultRecordDetailList().size();



    }

    @Override
    public int insertRecordAbnormal(AbnormalVo abnormalVo) {
        if (Objects.isNull(abnormalVo)) {
            log.error("faultRecordVO 为 null");
            return 0;
        }


        return faultRecordMapper.insertRecordAbnormal(abnormalVo);
    }

    @Override
    public List<FaultreportVO> selectFaultRecordListdataInfo(FaultreportVO faultRecord) {

        return faultRecordMapper.selectFaultRecordListdataInfo(faultRecord);
    }
}
